package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JWTUtil;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.SelectPrizeList;
import com.example.lotterysystem.service.IPictureService;
import com.example.lotterysystem.service.IPrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/1/29
 */
@Slf4j
@RestController
public class PrizeController {
    @Resource
    private IPictureService pictureService;

    @Resource
    private IPrizeService prizeService;

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * RequestPart：用于接收表单数据：multipart/form-data
     *
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(HttpServletRequest request,
                                          @Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        log.info("createPrize接收到参数：param: {}", JacksonUtil.writeValueAsString(param));
        String userToken = request.getHeader("user_token");
        Integer userId = JWTUtil.getUserIdFromToken(userToken);
        return CommonResult.success(prizeService.createPrize(userId, param, picFile));
    }

    /**
     * 分页查询奖品列表
     *
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<SelectPrizeList> selectPrizeByPage(HttpServletRequest request, PageParam param) {
        log.info("selectPrizeByPage 接收到参数：{}", JacksonUtil.writeValueAsString(param));
        String userToken = request.getHeader("user_token");
        Integer userId = JWTUtil.getUserIdFromToken(userToken);
        PageListDTO<PrizeDTO> pageListDTO = prizeService.selectPrizeByPage(userId, param);
        return CommonResult.success(convertToSelectPrizeList(pageListDTO));
    }

    private SelectPrizeList convertToSelectPrizeList(PageListDTO<PrizeDTO> pageListDTO) {
        if (pageListDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        SelectPrizeList prizeList = new SelectPrizeList();
        prizeList.setTotal(pageListDTO.getTotal());
        prizeList.setRecords(pageListDTO.getRecords()
                .stream()
                .map(prizeDTO -> {
                    SelectPrizeList.PrizeInfo prizeInfo = new SelectPrizeList.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPriceId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );
        return prizeList;
    }
}
